package com.movieknight;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;

import com.github.savvasdalkitsis.jtmdb.Movie;
import android.app.Application;
import android.widget.TextView;

import com.android.library.ImageDownloader;

// MovieKnightApp extends Application.
// It provides several global variables and functions.

public class MovieKnightApp extends Application {
	// Initialize TMDBMovieDownloader, ImageDownloader, and SimpleDateFormat
	public final static ImageDownloader imageDownloader = new ImageDownloader();
	public final static TMDBMovieDownloader movieDownloader = new TMDBMovieDownloader();
	public final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
	private Socket server = null;
	private String sessionID = "";
	private String username = "";
	private boolean tabsLaunched = false;
	private int errorCode = 0;
	
	public String getUsername(){
		return username;
	}
	
	public void setUsername(String username){
		this.username = username;
	}
	
	public int getErrorCode(){
		return errorCode;
	}
	
	public void setErrorCode(int errorCode){
		this.errorCode = errorCode;
	}
	
	public boolean isTabsLaunched(){
		return tabsLaunched;
	}
	
	public void setTabsLaunched(boolean tabsLaunched){
		this.tabsLaunched = tabsLaunched;
	}

    public Socket getServer() {
        return server;
    }

    public void setServer(Socket server) {
        this.server = server;
    }
	
    public String getSessionID(){
    	return sessionID;
    }
    
    public void setSessionID(String sessionID){
    	this.sessionID = sessionID;
    }
	@Override
	public void onCreate() {
		super.onCreate();
		// Initalize TMDB's API Key.
		com.github.savvasdalkitsis.jtmdb.GeneralSettings.setApiKey("c4c60182c2260bf1f49607744afd8e40");
	}
	
	
	public ImageDownloader getImageDownloader() {
		return imageDownloader;
	}
	
	// Fetches TMDB's Alternate movie title, if one exists.
	public String chooseSecondaryName(Movie movie, boolean setPrefix) {
		final String name = movie.getName();
		final String original = movie.getOriginalName();
		final String alternative = movie.getAlternativeName();
		String secondary;
		
		if(original.equalsIgnoreCase("null") && alternative.equalsIgnoreCase("null")) {
			return "null";
		}
		
		if (!original.equalsIgnoreCase(name) && !original.equalsIgnoreCase("null")) {
			secondary = original;
			if (setPrefix) {
				secondary = getString(R.string.prefixOriginal) + " " + secondary;
			}
			return secondary;
		}
		
		if (!alternative.equalsIgnoreCase(name) && !alternative.equalsIgnoreCase("null")) {
			secondary = alternative;
			if (setPrefix) {
				secondary = getString(R.string.prefixAlternative) + " " + secondary;
			}
			return secondary;
		}
		return "null";
		
	}
}